$(function() {
	createPlupload('uploadFile','platformRoom',uploadedImageCallback);
	
	$('#submitForm').validate({
		debug: true, //调试模式取消submit的默认提交功能   
        focusInvalid: false, 
        onkeyup: false,
        submitHandler: function(form){  
            if (typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') {
            	jQuery.data(this, "disabledOnSubmit", {submited : true});
            	form.submit();
            } else {
            	// repeat submit ignore;
            }
        },
        success:function(element){
        },
		errorPlacement : function(error, element) {
			if(error.text()){
				if(element.next("span[class='red']").size() == 0){
					element.after("<span class='red'>" + error.text() +"</span>");
				}else{
					element.next("span[class='red']").text(error.text());
				}
			}else{
				element.next("span[class='red']").remove();
			}
		},
        rules:{
            roomSortCode:{
                required:true,
                remote:{
                    url:appServer + '/goods/platformRoom/ajax/validateRoomSortCode.json',
                    type: "post",
                    dataType: "json",
                    data:{
                    	"roomSortCode":function(){return $("input[name='roomSortCode']").val();},
                        "id":function(){return ($("input[name='id']").val())?$("input[name='id']").val():"";}
					}
                }
			},
			roomName:{required:true},
			roomCode:{
				required:true,
				remote:{
					url:appServer + '/goods/platformRoom/uniqueCode.json',
					type: "post",
                    dataType: "json",
                    data:{
                    	  "roomCode":function(){return $("input[name='roomCode']").val();},
                    	  "id":function(){return ($("input[name='id']").val())?$("input[name='id']").val():"";}
                    	 }
				},
				isDigits:true
			},
			accessCode:{
				conditionRequired:(function(){
					if($("[name=type]").val() == "PRIVATE"){
						return true;
					}else{
						return false;
					}
				}),
				isFulString:true
			},
			logo:{required:true},
            classifyId:{required:true}
        },
        messages:{
            roomSortCode:{required:"房间排序码必填",remote:"房间排序码已存在"},
			roomName:{required:"房间名称必填"},
			roomCode:{required:"房间编号必填",remote:"房间编号已存在"},
			accessCode:{isFulString:"只允许输入字符(a-zA-Z0-9)"},
			logo:{required:"房间logo必填"}
        }
	});
	
	
	$("select[name=type]").bind("change",function(){
		initRoomTypeData(this.value);
	});
	initRoomTypeData($("input[name=type]").val());
});

function initRoomTypeData(roomType){
	if(roomType){
		$("tr.dnaymic").hide();
        $("tr.dynamic").not("tr." + roomType).find("input").val("");
		if(roomType == "PRIVATE"){
			$("tr.dnaymic."+roomType).show();
		}
        $("tr.dnaymicTwo").hide();
        //$("tr.dnaymicTwo").not("tr." + roomType).find("input").val("立即添加");
        if(roomType == "PRIVATE"){
            $("tr.dnaymicTwo."+roomType).show();
        }
		
	}
}

function deleteRecord(id){
	var r=confirm("确认删除？");
	if (r==true){
		window.location.href=appServer + "/goods/platformRoom/delete.htm?id="+id;
	}else{
		return;
	}
}

function toAddPage(classifyId){
	window.location.href = appServer + "/goods/platformRoom/add.htm?classifyId="+classifyId;
}

function uploadedImageCallback(data){
    if(data.status == 200 ){
        data = $.parseJSON(data.response);
        var imagePath = imageServer +'/' + data.result[0].smallImageUrl;
        var	innerHtml = '';
        innerHtml += '<img src="' + imagePath + '" style="width:88px; height:88px;"/>';
        innerHtml += '<br />';
        //图片赋值
        $('#imageLogoPositive').html(innerHtml);
        $('input[name=logo]').val(data.result[0].smallImageUrl);
    }
}

function changeClassifyId(classifyId) {
    if(classifyId != '') {
        jQuery.ajax({
            type:"post",
            url:appServer + "/goods/roomClassify/ajax/getClassify.htm",
            dataType:"json",
            data:{"classifyId":classifyId},
            success:function(message){
                var classifyList = message.classifyList;
                $('#classifyIdCheck').empty();
                $('#classifyIdCheck').prepend("<option value=''>请选择</option>");
                if(classifyList != null) {
                    for(var i=0;i<classifyList.length;i++ ) {
                        var classify = classifyList[i];
                        $('#classifyIdCheck').append("<option value='" + classify.id + "'>" +classify.classifyName  + "</option>");
                    }
                }
            },
            error:function(xhr, message, e){

            }
        });
    }else{
        $('#classifyIdCheck').empty();
        $('#classifyIdCheck').prepend("<option value=''>请选择</option>");
    }
}

function selectBrokerUsers() {
    var idList = new Array();
    idList = $("#userIds").val().split(",");
    if (idList.length == 5) {
        alert("一个房间最多添加5个免密访问");
    }else {
        window.open(appServer + '/goods/platformRoom/getBrokerUsers.htm?callBack=setBrokerUsers', 'newwindow', 'height=400, width=1000, top=20,left=150, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no').focus();
    }
}

function setBrokerUsers(json){
    debugger;
    var brokerUserIds = json.userId;
    var brokerFullNames = json.userName;
    var userIds = $("#userIds").val();
    if (userIds==""){
        $("#userIds").val(brokerUserIds);
        $("#fullNames").html(brokerFullNames);
    }else {
        var checks = true;
        var idList = new Array();
        var nameList = new Array();
        idList = $("#userIds").val().split(",");
        nameList = $("#fullNames").text().split(",");
        for(var i=0;i<idList.length;i++){
            if (idList[i]===brokerUserIds){
                checks = false;
            }
        }
        if (checks==true) {
            if (idList.length == 5) {
                alert("一个房间最多添加5个免密访问");
            } else if (idList.length > 0 && idList.length < 5) {
                idList[idList.length] = brokerUserIds;
                nameList[nameList.length] = brokerFullNames;
                var userIds = idList.join(",");
                var fullNames = nameList.join(",");
                $("#userIds").val(userIds);
                $("#fullNames").html(fullNames);
            }
        }else if(checks==false&&idList.length<5) {
            alert("经纪商已选择,请勿重复添加！");
        }
        else if(checks==false&&idList.length==5) {
            alert("一个房间最多添加5个免密访问");
        }
    }
}

function clearUserIds() {
    $("#userIds").val("");
    $("#fullNames").html("");
}